package cn.iocoder.yudao.module.data.third.eastMoney.request;

import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.module.data.third.IBaseRequest;
import cn.iocoder.yudao.module.data.third.IBaseResponse;
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.base.Joiner;

import java.util.Map;

/**
 * <p>
 *
 * </p>
 *
 * @author pokbrin
 * @date 2023-07-06 16:51
 */
public abstract class AbstractEastMoneyRequest<S extends IBaseResponse> implements IBaseRequest<S> {

    @Override
    public String name() {
        return "东方财富";
    }

    @Override
    public String url() {
        //序列化参数，读取注解的映射字段，并重新组装成bean
        String json = JsonUtils.toJsonPrettyString(this);
        Map<String, Object> bean = JsonUtils.parseObject(json, new TypeReference<Map<String, Object>>() {
        });

        // 移除值为空的属性，不然有些非必传参数不传要报NPE
        bean.entrySet().removeIf(entry -> entry.getValue() == null);
        String uri = Joiner.on('&').withKeyValueSeparator("=").join(bean);
        return StrUtil.format("{}?{}", host(), uri);
    }

    @Override
    @SuppressWarnings("unchecked")
    public Class<S> responseClass() {
        return (Class<S>) ClassUtil.getTypeArgument(this.getClass());
    }

}
